package com.west_traveler.find_page_fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.west_traveler.application.BaseApplication;
import com.west_traveler.entity.entity_editor.Discovery;
import com.west_traveler.entity.entity_editor.EditorRecommend;
import com.west_traveler.entity.entity_editor.Imagess;
import com.west_traveler.entity.entity_editor.Info;
import com.west_traveler.entity.entity_editor.Special;
import com.west_traveler.entity.entity_project.Hot;
import com.west_traveler.entity.entity_project.Like;
import com.west_traveler.entity.entity_project.LikeProject;
import com.west_traveler.entity.entity_project.Novel;
import com.west_traveler.entity.entity_project.Project;
import com.west_traveler.recyclerview_adapter.RecommentFraRecyclerViewAdapter;
import com.west_traveler.utils.CommonUtils;
import com.west_traveler.ximalayafm.R;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 发现页推荐Fragment
 * A simple {@link Fragment} subclass.
 */
public class RecommendFragment extends Fragment implements Callback<Project> {

    private RecyclerView recyclerView;
    private RecommentFraRecyclerViewAdapter adapter;
    private List total = new ArrayList();
    private EditorRecommend editorRecommendAlbums;
    private Special specialColumn;
    private Info body;

    public static RecommendFragment getInstance() {
        return new RecommendFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_recommend, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = ((RecyclerView) view.findViewById(R.id.recycleview_Recommend_Fra_id));

        //数据源
        BaseApplication application = (BaseApplication) getActivity().getApplication();

        //http://mobile.ximalaya.com/mobile/discovery/v3/recommends?
        // channel=and-a1&device=android&includeActivity=true&includeSpecial=true&scale=2&version=4.3.98
        application.getService().getViewPagerUsedImg("and-a1", "android", true, true, 2, "4.3.98").enqueue(new Callback<Info>() {
            @Override
            public void onResponse(Call<Info> call, Response<Info> response) {
                body = response.body();
                List<Imagess> imagessList = body.getFocusImages().getList();//焦点图
                Discovery discoveryColumns = body.getDiscoveryColumns();//惊奇发现  顶部viewpager使用的8个

                //精品听单
                specialColumn = body.getSpecialColumn();

                //小编推荐
                editorRecommendAlbums = body.getEditorRecommendAlbums();

//                total.add(body);
//                total.add(specialColumn);
//                total.add(editorRecommendAlbums);
                Log.i("TAG", "" + body.getEditorRecommendAlbums().getList().size() + "----->>>>" + body.toString());
            }

            @Override
            public void onFailure(Call<Info> call, Throwable t) {

            }
        });

        application.getService().getList("android").enqueue(this);
    }

    @Override
    public void onResponse(Call<Project> call, Response<Project> response) {
        Project body1 = response.body();
        //adapter.addNovel(hot);
        Hot hot = body1.getHotRecommends();
        Like guess = body1.getGuess();  //猜你喜欢
        List<LikeProject> likeProjectList = guess.getList();

        List<Novel> novelList = hot.getList(); //听类型的 20个

        total.add(body);//焦点图和惊奇发现8个
        total.add(guess); //猜你喜欢
        total.add(editorRecommendAlbums);//小编推荐
        total.add(specialColumn);//精品听单

        for (int i = 0; i < novelList.size(); i++) {
            total.add(novelList); //听的类型 20个
        }
        adapter = new RecommentFraRecyclerViewAdapter(getActivity(), total);
        recyclerView.setAdapter(adapter);
    }


    @Override
    public void onFailure(Call<Project> call, Throwable t) {

    }
}
